package com.frcdi.qms.common.config;


import com.frcdi.qms.common.constant.CacheConstants;
import com.frcdi.qms.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
 * @ClassName EmailConfig
 * @Author Mr.Han
 * @create 2023/8/3 13:18
 * @update 2024-5-14 10:04:37
 */
@Configuration
public class EmailConfig {
    @Autowired
    private RedisCache redisCache;

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(redisCache.getCacheObject(getCacheKey("mail_host")));
        mailSender.setUsername(redisCache.getCacheObject(getCacheKey("mail_username")));
        mailSender.setPassword(redisCache.getCacheObject(getCacheKey("mail_password")));

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");

        return mailSender;
    }

    public  String getSendName() {
        return redisCache.getCacheObject(getCacheKey("mail_username"));
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return CacheConstants.SYS_CONFIG_KEY + configKey;
    }
}
